package cn.changgou.util;

import cn.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/*实现FastDFS文件管理*/
public class FastDFSUtil {

    /*加载Tracker链接信息*/
    static {
        try {
            //查找classpath下的文件路径
            String filename = new ClassPathResource("fdfs_client.conf").getPath();
            //加载Tracker链接信息
            ClientGlobal.init(filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*获取TrackerServer*/
    public static TrackerServer getTrackerServer() throws Exception{
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient访问TrackerServer服务，获取连接信息
        return trackerClient.getConnection();
    }

    /*获取StorageClient*/
    public static StorageClient getStorageClient(TrackerServer trackerServer) throws Exception{
        //通过TrackerServer的连接信息可以获取Storage的连接信息，创建StorageClient对象存储Storage的连接信息
        return new StorageClient(trackerServer, null);
    }

    /*文件上传*/
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception{
        TrackerServer trackerServer = getTrackerServer();
        StorageClient storageClient = getStorageClient(trackerServer);
        /* 通过StorageClient访问Storage，实现文件上传，并且获取文件上传后的存储信息
            1.上传文件的字节数组
            2.文件的拓展名
            3.附加参数，例如：拍摄地址-北京*/
        //附加参数
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("拍摄地址", "北京");
        String[] uploads = storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), meta_list);
        return uploads;
    }

    /*获取文件信息*/
    public static FileInfo getFile(String groupName, String remoteFileName) throws Exception{
        TrackerServer trackerServer = getTrackerServer();
        StorageClient storageClient = getStorageClient(trackerServer);
        //获取文件信息
        return storageClient.get_file_info(groupName, remoteFileName);
    }

    /*文件下载*/
    public static InputStream downFile(String groupName, String remoteFileName) throws Exception{
        TrackerServer trackerServer = getTrackerServer();
        StorageClient storageClient = getStorageClient(trackerServer);
        //文件下载
        byte[] buffer = storageClient.download_file(groupName, remoteFileName);
        return new ByteArrayInputStream(buffer);
    }

    /*文件删除*/
    public static void deleteFile(String groupName, String remoteFileName) throws Exception{
        TrackerServer trackerServer = getTrackerServer();
        StorageClient storageClient = getStorageClient(trackerServer);
        //文件删除
        storageClient.delete_file(groupName, remoteFileName);
    }

    /*获取Storage信息*/
    public static StorageServer getStorages() throws Exception{
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        //获取Storage信息
        return trackerClient.getStoreStorage(trackerServer);
    }

    /*获取Storage的IP和端口信息*/
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) throws Exception{
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        //获取Storage信息
        return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
    }

    /*获取Tracker信息*/
    public static String getTrackerInfo() throws Exception{
        TrackerServer trackerServer = getTrackerServer();
        //Tracker的IP、HTTP端口
        String ip = trackerServer.getInetSocketAddress().getHostString();
        int tracker_http_port = ClientGlobal.getG_tracker_http_port();
        String url = "http://" + ip + ":" + tracker_http_port;
        return url;
    }

    public static void main(String[] args) throws Exception {
        /*//文件获取
        FileInfo fileInfo = getFile("group1", "M00/00/00/wKjThGBHKqKAcQXWAAAF7t0PiiM791.jpg");
        System.out.println(fileInfo.getSourceIpAddr());
        System.out.println(fileInfo.getFileSize());*/

        /*//文件下载
        InputStream is = downFile("group1", "M00/00/00/wKjThGBHKqKAcQXWAAAF7t0PiiM791.jpg");
        FileOutputStream os = new FileOutputStream("D:/1.jpg");
        byte[] buffer = new byte[1024];
        while (is.read(buffer)!=-1){
            os.write(buffer);
        }
        os.flush();
        os.close();
        is.close();*/

        /*//文件删除
        deleteFile("group1", "M00/00/00/wKjThGBHIPmAMM4uAAAGbPKatq8946.jpg");*/

        /*//获取Storage信息
        StorageServer storageServer = getStorages();
        System.out.println("storage下标：" + storageServer.getStorePathIndex());
        System.out.println("storageIP端口：" + storageServer.getInetSocketAddress().getHostString());*/

        /*//获取Storage组的IP和端口信息
        ServerInfo[] groups = getServerInfo("group1", "wKjThGBHKqKAcQXWAAAF7t0PiiM791.jpg");
        for (ServerInfo group : groups) {
            System.out.println(group.getIpAddr());
            System.out.println(group.getPort());
        }*/

        //获取Tracker信息
        System.out.println(getTrackerInfo());

    }
}
